//
// Created by papupupu on 12/24/2022.
//
//successful
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"

int
main(int argc,char* args[])
{
    int fd1[2], fd2[2];
    if(pipe(fd1) == -1 || pipe(fd2) == -1){
        printf("pipe falid!\n");
        exit(1);
    }
    int pid = fork();
    if(pid == -1){
        printf("fork falid!\n");
        exit(1);
    }

    else if(pid > 0){
        close(fd1[0]);
        close(fd2[1]);
        char buffer[10];
        write(fd1[1], "ping", sizeof("ping"));
        wait((int*) 0);
        read(fd2[0], buffer, sizeof("pong"));
        printf("<%d>:received %s\n", getpid(), buffer);
        exit(0);
    }

    else{
        close(fd1[1]);
        close(fd2[0]);
        char buffer[10];
        read(fd1[0], buffer, sizeof("ping"));
        printf("<%d>:received %s\n", getpid(), buffer);
        write(fd2[1],"pong",sizeof("pong"));
        exit(0);
    }

}
